package com.construction.api.controller;

import com.construction.api.config.annotation.ValidToken;
import com.construction.api.domain.dto.SysUserDTO;
import com.construction.api.domain.model.header.Header;
import com.construction.api.domain.vo.TokenVO;
import com.construction.api.service.LoginService;
import com.construction.api.util.JwtTokenUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author dybin
 * @Date: 2021/7/1 23:02
 */
@RestController
@RequiredArgsConstructor
@ValidToken
public class LoginController extends BaseController {

    private final LoginService loginService;
    private final JwtTokenUtil jwtTokenUtil;

    @GetMapping("/login")
    public TokenVO login(SysUserDTO sysUserDTO) {
        return loginService.login(sysUserDTO);
    }

    @PutMapping("/refresh-token")
    public TokenVO refresh(HttpServletRequest request) {
        Header header = Header.of(request, jwtTokenUtil);
        return loginService.refresh(header.getToken());
    }
}
